import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import vuex from '@/store'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      hideNB: true
    }
  },
  {
    path: '/navbar',
    name: 'navbar',
    component: () => import('@/views/test/navbar.vue'),
    meta: {
      title: '导航栏',
      bgColor: '#469eff'
    }
  },
  {
    path: '/rem',
    name: 'rem',
    component: () => import('@/views/test/rem.vue'),
    meta: {
      title: 'rem适配'
    }
  },
  {
    path: '/echarts',
    name: 'echarts',
    component: () => import('@/views/test/echarts.vue'),
    meta: {
      title: 'echarts'
    }
  },
  {
    path: '/store',
    name: 'store',
    component: () => import('@/views/test/store.vue'),
    meta: {
      title: 'echarts'
    }
  },
  {
    path: '/amap',
    name: 'amap',
    component: () => import('@/views/test/amap.vue'),
    meta: {
      title: '高德地图'
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('@/views/Mine.vue'),
    meta: {
      title: '我的',
      hideNB: true
    }
  },
  {
    path: '/component',
    name: 'component',
    component: () => import('@/views/test/component.vue'),
    meta: {
      title: '自动导入组件'
    }
  },
  {
    path: '/gestureImg',
    name: 'gestureImg',
    component: () => import('@/views/test/gestureImg.vue'),
    meta: {
      title: '手势操作'
    }
  },
  {
    path: '/gesturePopup',
    name: 'gesturePopup',
    component: () => import('@/views/test/gesturePop.vue'),
    meta: {
      title: '手势操作'
    }
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('@/views/test/list.vue'),
    meta: {
      title: 'prolist组件'
    }
  },
  {
    path: '/refs',
    name: 'refs',
    component: () => import('@/views/test/refs.vue'),
    meta: {
      title: '循环列表生成ref'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
});
router.beforeEach(async (to, from, next) => {
  const title = to.meta && to.meta.title;
  if(title){
    vuex.commit('set_navBarTitle', title);
  } else {
    vuex.commit('set_navBarTitle', '');
  }
  next();
});

export default router
